﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace RM2012Mobile
{
    public partial class frmTCPIP : Form
    {
        public frmTCPIP()
        {
            InitializeComponent();
        }

        private void btnSendToServer_Click(object sender, EventArgs e)
        {
            TCPIP objTcp = new TCPIP();
            Thread tr = new Thread(new ThreadStart(createStreamToServer));
            tr.Start();
        }

        public void createStreamToServer()
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
                Console.WriteLine("Connecting ...");
                tcpClient.Connect(IPAddress.Parse("192.168.150.1"), 500);
                Console.WriteLine("Connected.");
                Stream streamClient = tcpClient.GetStream();
                ASCIIEncoding ae = new ASCIIEncoding();
                byte[] bytes = ae.GetBytes("tienit");
                Console.WriteLine("Transmitting ...");
                streamClient.Write(bytes, 0, bytes.Length);

                //nhan ve
                byte[] bytesReceived = new byte[100];
                int result = streamClient.Read(bytesReceived, 0, 100);
                for (int i = 0; i < result; i++)
                {
                    Console.Write(Convert.ToChar(bytesReceived[i]));
                }
                tcpClient.Close();
            }
            catch (System.Exception ex)
            {
                //Console.WriteLine(ex.StackTrace);
                MessageBox.Show(ex.Message);
            }
        }

        private void frmTCPIP_Load(object sender, EventArgs e)
        {
            createStream();
            //createStreamToServer();
        }

        public void createStream()
        {
            try
            {
                EndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.150.1"), 500);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Console.WriteLine("Connecting ...");
                socket.Connect(endPoint);
                Console.WriteLine("Connected.");
                if (socket.Connected)
                {
                    //XML data
                    //string xmlData = "<menu>";
                    //xmlData += "<item>" + "Lẩu gà" + "</item>";
                    //xmlData += "<item>" + "Lẩu bò" + "</item>";
                    //xmlData += "<item>" + "Cá hấp bia" + "</item>";
                    //xmlData += "<item>" + "Lẩu dê" + "</item>";
                    //xmlData += "<item>" + "Rượt XO" + "</item>";
                    //xmlData += "</menu>";
                    socket.Send(Encoding.Unicode.GetBytes("tienit from client"));
                    Console.WriteLine("Transmitting ...");
                    //Receive data from Server
                    byte[] buffer = new byte[100];
                    socket.Receive(buffer, SocketFlags.None);
                    Console.WriteLine("Received from Server.");
                    string strReceived = Encoding.Unicode.GetString(buffer, 0, 100);
                    Console.WriteLine(strReceived);
                    //MessageBox.Show(strReceived);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}